package com.zkingsoft.actions.customer;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tencent.protocol.simple_protocol.SimpleProtol;
import com.zkingsoft.actions.common.BaseController;
import com.zkingsoft.anotations.RemoveRequestToken;
import com.zkingsoft.anotations.SaveRequestToken;
import com.zkingsoft.constance.Dictionary;
import com.zkingsoft.exception.GlobleException;
import com.zkingsoft.model.Fy;
import com.zkingsoft.model.TopRule;
import com.zkingsoft.model.ZdOrder;
import com.zkingsoft.plugin.pojo.PaginationVO;
import com.zkingsoft.plugin.util.LogUtil;
import com.zkingsoft.plugin.util.WebUtil;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.services.FyService;
import com.zkingsoft.services.TopRuleService;
import com.zkingsoft.services.ZdOrderService;

/**
 * This field was generated by Zking.software.Codegen.
 * 
 * @date 2016-10-07 08:53
 */
@Controller
@RequestMapping(value = "customer/fy")
public class CustomerFyController extends BaseController {

	@Resource
	private FyService currentService;

	@Resource
	private TopRuleService topruleService;
	@Resource
	private ZdOrderService zdOrderService;

	/**
	 * 列表显示
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(Fy fy, PaginationVO pageVo) {
		return showList(currentService, fy, pageVo);
	}

	/**
	 * 新增或者修改页面
	 */
	@RemoveRequestToken
	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(Fy fy) {
		if (fy.getId() != null) {
			return modify(currentService, fy, "房源");
		} else {

			// 新增房源信息
			fy.setFySource(Dictionary.FY_RESOURCE_KHFB);
			
			
			// 设置主图
			if (fy.getPhotoList() != null) {
				fy.setFyImage(fy.getPhotoList().get(1).getUrl());
			}
			int i = currentService.add(fy);
			if (i <= 0) {
				throw new GlobleException("订单创建失败");
			}
			// 跳转的支付接口
			String page = "";
			// 是否存在置顶计划 计划置顶=开始时间减去结束时间*单价=需要支付的金额
			if (fy.getGlTopRuleId() != null) {
				// 查询置顶对象
				TopRule topRule = topruleService.findById(fy.getGlTopRuleId());
				// 如果规则存在
				if (topRule != null) {
					// 存在房源置顶计划 创建订单
					ZdOrder order = new ZdOrder();
					order.setOrderNo("NO" + System.currentTimeMillis());
					order.setOrderTime(new Date());
					order.setPayWay("微信");
					order.setSource("房源置顶");
					order.setSpInfo(topRule.getName());
					order.setTotal(topRule.getPrice());
					order.setUserId(getMe().getId());
					order.setYxzt("有效");
					order.setRemark("待支付");
					order.setFyId(fy.getId());
					// 在数据库中新增订单
					int j = zdOrderService.add(order);
					if (j > 0) {
						page = order.getOrderNo();
					} else {
						throw new GlobleException("订单创建失败");
					}
				}
			}
			// 查询是否支付成功，支付成功后设置房源置顶状态
			return new AjaxResult(AjaxResult.STATUS_OK, page, "房源发布成功");
		}
	}

	/**
	 * 
	 * @Description: 进入修改界面
	 * @author:姜友瑶
	 * @param id
	 * @param flag
	 *            1 表示修改 2表示修改审核
	 * @return 返回类型 String
	 * @date 2016年10月9日
	 */
	@SaveRequestToken
	@RequestMapping(value = "/editForm")
	public String editForm(Long id, int flag) {
		Fy fy = currentService.findById(id);
		WebUtil.getRequest().setAttribute("obj", fy);
		WebUtil.getRequest().setAttribute("flag", flag);
		String path = getPath(fy);
		return "customer/" + path;
	}

	/**
	 * 进入新增界面
	 */
	@SaveRequestToken
	@RequestMapping(value = "/addForm")
	public String addForm(Long id) {
		return "customer/publish-fy";
	}

	/**
	 * 进入新增类型选择界面
	 */
	@SaveRequestToken
	@RequestMapping(value = "/fyClass")
	public String fyClass() {
		// 判断是否进行了自动登录
		if (WebUtil.isWeiXin()&&WebUtil.getSession().getAttribute(Dictionary.AUTO_LOGINED) == null) {
			LogUtil.info(this.getClass(), "callback url= " + Dictionary.WXAUTODL_CALLBACK);
			WebUtil.getSession().setAttribute("callback", WebUtil.getCurrentPageUrl());
			return "redirect:" + SimpleProtol.getUserSq(Dictionary.WXAUTODL_CALLBACK, "snsapi_base",
					"" + System.currentTimeMillis());
		}
		return "customer/house-class";
	}

	/**
	 * 进入新增界面2
	 */
	@SaveRequestToken
	@RequestMapping(value = "/addForm2")
	public @ResponseBody AjaxResult addForm2(Fy fy) {

		// 设置房源信息和房源的跳转页面
		WebUtil.getSession().setAttribute("obj", fy);
		String path = getPath(fy);
		// 获取计划置顶，和立即置顶信息
		TopRule topRule = new TopRule();
		topRule.setTopType(Dictionary.TOP_TYPE_LKZD);
		topRule.setPublishStatus(Dictionary.PUBLISH_STATUS_SJ);
		List<TopRule> lkzd = topruleService.findByModel(topRule);

		topRule.setTopType(Dictionary.TOP_TYPE_JHZD);
		List<TopRule> jhzd = topruleService.findByModel(topRule);

		WebUtil.getSession().setAttribute("lkzd", lkzd);
		WebUtil.getSession().setAttribute("jhzd", jhzd);

		return new AjaxResult(AjaxResult.STATUS_OK, path, "正在跳转");
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		return remove(currentService, keys);
	}

	/**
	 * 
	 * @Description: 根据房源类型跳转到不同的页面
	 * @author:姜友瑶
	 * @param fy
	 * @return 返回类型 String
	 * @date 2016年10月9日
	 */
	private String getPath(Fy fy) {
		String path = "";
		// 二手房
		if (fy.getFyKinds().equals(Dictionary.FY_KINDS_ESF) && fy.getFyGq().equals(Dictionary.FY_GQ_CS)) {
			path = "fy-xfcsform";
		} else if (fy.getFyKinds().equals(Dictionary.FY_KINDS_ESF) && fy.getFyGq().equals(Dictionary.FY_GQ_QG)) {
			path = "fy-xfqgform";

			// 写字楼
		} else if (fy.getFyKinds().equals(Dictionary.FY_KINDS_XZL)
				&& (fy.getFyGq().equals(Dictionary.FY_GQ_QG) || fy.getFyGq().equals(Dictionary.FY_GQ_QZ))) {
			path = "fy-xzlqgform";
		} else if (fy.getFyKinds().equals(Dictionary.FY_KINDS_XZL)
				&& (fy.getFyGq().equals(Dictionary.FY_GQ_CS) || fy.getFyGq().equals(Dictionary.FY_GQ_CZ))) {
			path = "fy-xzlcsform";

			// 出租房
		} else if (fy.getFyKinds().equals(Dictionary.FY_KINDS_CZF) && fy.getFyGq().equals(Dictionary.FY_GQ_CZ)) {
			path = "fy-czfczform";
		} else if (fy.getFyKinds().equals(Dictionary.FY_KINDS_CZF) && fy.getFyGq().equals(Dictionary.FY_GQ_QZ)) {
			path = "fy-czfqzform";

			// 商铺
		} else if (fy.getFyKinds().equals(Dictionary.FY_KINDS_SP) && (fy.getFyGq().equals(Dictionary.FY_GQ_ZR)
				|| fy.getFyGq().equals(Dictionary.FY_GQ_CZ) || fy.getFyGq().equals(Dictionary.FY_GQ_CS))) {
			path = "fy-spzrform";
		} else if (fy.getFyKinds().equals(Dictionary.FY_KINDS_SP)
				&& (fy.getFyGq().equals(Dictionary.FY_GQ_QG) || fy.getFyGq().equals(Dictionary.FY_GQ_QZ))) {
			path = "fy-spqgform";

			// 厂房
		} else if (fy.getFyKinds().equals(Dictionary.FY_KINDS_CF) && (fy.getFyGq().equals(Dictionary.FY_GQ_CS)
				|| fy.getFyGq().equals(Dictionary.FY_GQ_CZ) || fy.getFyGq().equals(Dictionary.FY_GQ_ZR))) {
			path = "fy-cfcsform";
		} else if (fy.getFyKinds().equals(Dictionary.FY_KINDS_CF)
				&& (fy.getFyGq().equals(Dictionary.FY_GQ_QZ) || fy.getFyGq().equals(Dictionary.FY_GQ_QG))) {
			path = "fy-cfqgform";
		}
		return path;
	}

	/**
	 * @Description:用户发布的房源
	 * @author:姜友瑶
	 * @param user
	 * @return 返回类型 AjaxResult
	 * @date 2016年10月12日
	 */
	@RequestMapping(value = "/findMyFy")
	public @ResponseBody AjaxResult findMyFy(Fy fy, PaginationVO pageVo) {
		fy.setYxzt("有效");
		fy.setGlUserId(getMe().getId());
		List<Fy> fyList = currentService.findInPage(fy, pageVo);
		return new AjaxResult(AjaxResult.STATUS_OK, null, "查询成功", fyList, currentService.findTotal(fy));
	}

	/**
	 * 刷新房源信息
	 */
	@RequestMapping(value = "/flushFyTime")
	public @ResponseBody AjaxResult flushFyTime(Long id) {
		Fy fy = currentService.findById(id);
		fy.setGlCreateTime(new Date());
		currentService.modify(fy);
		return new AjaxResult(AjaxResult.STATUS_OK, null, "刷新成功");
	}

}